<!--
 * @Description: 输入数量
 * @Author: liyujie
 * @Date: 2021/8/20
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/8/20
-->
<template>
    <section class="wrapper">
        <el-dialog
            :visible.sync="dialogVisible"
            width="410px"
            :title="title"
            :close-on-click-modal="false"
            @close="$emit('close')"
        >
            <div class="dialog-content">
                <el-input-number
                    v-model="inputNumber"
                    :step="1"
                    step-strictly
                    :controls="true"
                    :min="-1"
                    size="medium"
                />
                <span style="margin-left: 12px">件</span>
            </div>
            <div class="dialog-footer">
                <el-button
                    size="small"
                    style="width: 100px"
                    @click="close"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="small"
                    style="width: 100px;margin-left: 20px"
                    @click="handleSubmit()"
                >
                    确认
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import Vue from "vue";

const Dialog = {
    name: "InputNumber",
    props: {
        title: {
            type: String,
            default: ''
        },
        defaultValue: {
            type: Number,
            default: 1
        }
    },
    data() {
        return {
            dialogVisible: false,
            inputNumber: 1,
        };
    },
    watch: {
        defaultValue: {
            handler(val) {
                this.inputNumber = val;
            },
            immediate: true
        }
    },
    methods: {

        /**
         * 显示弹窗
         */
        show() {
            this.dialogVisible = true;
        },

        /**
         * 显示弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * 关闭弹窗前
         */
        beforeClose() {
            this.inputNumber = 1;
            this.$emit('close');
        },

        /**
         * 选择秒杀时间
         */
        handleSubmit() {
            this.$emit('confirm', this.inputNumber);
        },
    }
};

export default Dialog;

const inputNumberDialog = function (config) {
    return new Promise((resolve) => {
        const DialogConstructor = Vue.extend(Dialog);
        const instance = new DialogConstructor({
            propsData: config,
        }).$mount();

        instance.$on("close", function () {
            setTimeout(() => {
                document.body.removeChild(instance.$el);
                instance.$destroy();
            }, 300);
            resolve(false);
        });
        instance.$on("confirm", function (data) {
            instance.close();
            resolve(data);
        });
        document.body.appendChild(instance.$el);
        instance.dialogVisible = true;
    });
};

export { inputNumberDialog };
</script>

<style scoped lang="less">
.dialog-content {
    display: flex;
    align-items: center;
    justify-content: center;
}
.dialog-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 24px;
}
/deep/ .el-dialog__header {
    padding-top: 10px;
    box-shadow:0 1px 0 0 rgba(233,233,233,1);
    display: flex;
    align-items: center;
    background: #FAFAFA;
    border-radius: 4px 4px 0 0;
    .el-dialog__title {
        font-weight: 500;
        color: #333;
        font-size: 14px;
    }
    .el-icon-close:before {
        color: rgb(194,194,194);
        font-size: 17px;
        font-weight: bold;
    }
    .el-dialog__headerbtn {
        top: 14px;
    }
}
/deep/ .el-dialog__body {
    padding: 24px 32px;
}
</style>
